/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.ui.properties.tabbed;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.ValueSpecification;

import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.diagram.sequence.ui.composites.LifelineSelectorComposite;
import com.cea.papyrus.umlutils.Element;

/**
 * Property view section to set the selector {@link ValueSpecification} of a {@link Lifeline}
 * 
 */
public class LifelineSelectorSection extends PropertyViewSection {
	
	/**
	 * Dedicated composite to display selector.
	 */
	private LifelineSelectorComposite selectorComposite;
	
    /**
     * Create contents.
     * 
     * @param parent Composite
     * @param tabbedPropertySheetPage TabbedPropertySheetPage
     */
    @Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage){
        super.createControls(parent, tabbedPropertySheetPage);
                        
        parent.setLayout(new FormLayout());
		FormData data;
		
		data = new FormData();
		data.right = new FormAttachment(100, 0);
		data.left = new FormAttachment(0, 0);
		
		//table for ClassifierBehavior
		selectorComposite = new LifelineSelectorComposite("Lifeline selector");
		selectorComposite.createContent(parent, tabbedPropertySheetPage);
		selectorComposite.setLayoutData(data);
     }
    
    /**
     * Pass on the selected input to sub composites.
     * 
     * @param part IWorkbenchPart.
     * @param selection ISelection.
     */
    @Override
	public void setInput(IWorkbenchPart part, ISelection selection){
        super.setInput(part, selection);
               
        Assert.isTrue(selection instanceof IStructuredSelection);
        Object input = ((IStructuredSelection)selection).getFirstElement();
        
        if (input instanceof IUMLElementEditPart) {
         	org.eclipse.uml2.uml.Element element = 
         		((IUMLElementEditPart<?>)input).getUmlElement();
        	if (element instanceof Lifeline) {
           		Element utilElement = new Element((Lifeline) element);
           		selectorComposite.setElement(utilElement);
           		selectorComposite.setContentProvider(modelManager);
           		selectorComposite.setLabelProvider(modelManager);
         	}
        }
    }


    /**
     * Refreshes contents.
     */
    @Override
	public void refresh(){
    	selectorComposite.refresh();
     }

    /**
     * Disposes of contents. 
     */
    @Override
    public void dispose(){
    	super.dispose();
    	
    	if(selectorComposite != null)
    		selectorComposite.dispose();
    }
}

